package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.basic.web.service.LoginService;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import com.tarena.lbs.pojo.passport.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "用户登录模块")
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;


    @ApiOperation("用户登录接口")
    @PostMapping("/passport/admin/login")
    public Result<LoginVO> adminLogin (AdminLoginParam param)throws BusinessException{
        log.debug("用户登录接收到的参数 param:{}",param);
        //调用业务层,判断登录结果,登路成功,回查询到业务结果 AdminBO
        AdminBO adminBO = loginService.adminDoLogin(param);
        //控制层将BO转化为VO
        LoginVO vo = new LoginVO();
        BeanUtils.copyProperties(adminBO,vo);
        return new Result<>(vo);
    }

    @PostMapping("/passport/user/login")
    public Result<LoginVO> userLogin(UserLoginParam param) throws  BusinessException{
        log.debug("小程序登录接口入参:UserLoginParam :{}",param);
        UserBO userBO = loginService.userDoLogin(param);
        LoginVO vo = new LoginVO();
        BeanUtils.copyProperties(userBO,vo);
        return new Result<>(vo);
    }
}
